package com.loyal.camerax.analyzer;

import android.graphics.ImageFormat;
import android.media.Image;
import android.util.Size;

import androidx.annotation.NonNull;
import androidx.camera.core.ImageAnalysis;
import androidx.camera.core.ImageProxy;

import com.loyal.camerax.utils.ImageUtil;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 实时预览 Analyzer处理
 * <a href="https://blog.csdn.net/xj932956499/article/details/121859881">人脸检测</a>
 */
public class MyAnalyzer implements ImageAnalysis.Analyzer {
    private byte[] y;
    private byte[] u;
    private byte[] v;
    private ReentrantLock lock = new ReentrantLock();
    private Object mImageReaderLock = 1;//1 available,0 unAvailable
    private long lastDrawTime = 0L;
    private long timerSpace = 300L; // 识别间隔

    public int rational;
    private Size mImageReaderSize;
    private byte[] nv21 = null; // 实时nv21图像流

    @Override
    public void analyze(@NonNull ImageProxy image) {
//            int rotationDegrees = image.getImageInfo().getRotationDegrees();
            synchronized (mImageReaderLock) {
                /*识别频率Start 和状态*/
                long start = System.currentTimeMillis();
                if (start - lastDrawTime < timerSpace || !mImageReaderLock.equals(1)) {
                    image.close();
                    return;
                }
                lastDrawTime = System.currentTimeMillis();
                /*识别频率End*/

                //判断YUV类型，我们申请的格式类型是YUV_420_888
                if (ImageFormat.YUV_420_888 == image.getFormat()) {
                     ImageProxy.PlaneProxy[] planes = image.getPlanes();
                    if (mImageReaderSize == null) {
                        mImageReaderSize = new Size(planes[0].getRowStride(), image.getHeight());
                    }

                    lock.lock();
                    if (y == null) {
                        y = new byte[planes[0].getBuffer().limit() - planes[0].getBuffer().position()];
                        u = new byte[planes[1].getBuffer().limit() - planes[1].getBuffer().position()];
                        v = new byte[planes[2].getBuffer().limit() - planes[2].getBuffer().position()];
                    }
                    //从planes中分别获取y、u、v 变量数据
                    if (planes[0].getBuffer().remaining() == y.length) {
                        planes[0].getBuffer().get(y);
                        planes[1].getBuffer().get(u);
                        planes[2].getBuffer().get(v);

                        if (nv21 == null) {
                            nv21 = new byte[planes[0].getRowStride() * image.getHeight() * 3 / 2];
                        }

                        if (nv21 != null && (nv21.length != planes[0].getRowStride() * image.getHeight() * 3 / 2)) {
                            return;
                        }
                        // 回传数据是YUV422
                        if (y.length / u.length == 2) {
                            ImageUtil.yuv422ToYuv420sp(y, u, v, nv21, planes[0].getRowStride(), image.getHeight());
                        }
                        // 回传数据是YUV420
                        else if (y.length / u.length == 4) {
                            nv21 = ImageUtil.yuv420888ToNv21(image,null);
                        }

                        //调用Arcsoft算法,获取人脸特征
                        //getFaceInfo(nv21);
                    }
                    lock.unlock();
                }
            }

        //一定要关闭
        image.close();
    }

}
